<?php
// settings include
include ("../config.php");

//page def
//the page title
$title = "Edit Recipe";

// dB connection
include ("$install_dir/include/db.php");

//adds the header
include ("$install_dir/include/head.php");

//start page
?>
<body>
<center>
<table class="main_table">
<tr>
<th colspan="2" class="logo"><img src="<?php echo $install_url;?>/images/logo.png" alt="My Personal Cookbook" align="left" /></th>
</tr>
<tr>
<td class="menu">
<?php
//adds menu
include ("$install_dir/include/menu.php");

?>
</td>
<td class="page">
<div class = "title">:. <?php echo $title;?> .:</div>
<div class = "post_text">
<?php
if ($mode == NULL && $rec_id == NULL) {
   echo "Error no rec_id!<br />\n";
   echo "Error no mode!";
}
elseif ($mode == "list") {
	 echo "<b>Select a catigory...</b><br />\n<br />\n";
	 $query = "SELECT * FROM recipe GROUP BY rec_cat ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<a href=\"$install_url/admin/edit.php?mode=listcat&rec_cat=$row[rec_cat]\">$row[rec_cat]</a><br />\n";
	 }
	 echo "<br />\n<b><a href=\"$install_url/admin/edit.php?mode=listall\">List All</a></b><br />\n<b><a href=\"$install_url/admin/index.php\">Back to admin index.</a></b><br />\n";
}

elseif ($mode == "listcat") {
	 echo "<b>Select a recipe...</b><br />\n<br />\n";
	 $query = "SELECT * FROM recipe WHERE rec_cat='$rec_cat'";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<a href=\"$install_url/admin/edit.php?rec_id=$row[rec_id]\">$row[rec_name]</a><br />\n";
	 }
	 echo "<br />\n<b><a href=\"$install_url/admin/edit.php?mode=listall\">List All</a></b><br />\n<b><a href=\"$install_url/admin/edit.php?mode=list\">Back to list of catigories.</a></b><br />\n";
}

elseif ($mode == "listall") {
	 echo "<b>Select a recipe...</b><br />\n<br />\n";
	 $query = "SELECT * FROM recipe GROUP BY rec_name ASC";
   	 $result = mysql_query($query) or die(mysql_error());
	 while ($row = mysql_fetch_array($result)) {
	 echo "<a href=\"$install_url/admin/edit.php?rec_id=$row[rec_id]\">$row[rec_name]</a><br />\n";
	 }
	 echo "<br />\n<b><a href=\"$install_url/admin/edit.php?mode=list\">Back to list of catigories.</a></b><br />\n";
}
	 
elseif ($mode == NULL && $rec_id != NULL) {
     $query = "SELECT * FROM recipe WHERE rec_id=$rec_id";
   	 $result = mysql_query($query) or die(mysql_error());
   	 while ($row = mysql_fetch_array($result)) {
?>

<script src="speller/spellChecker.js"></script>
<script>
function openSpellChecker() {
	var speller = new spellChecker();
	speller.spellCheckAll();
}
</script>

<script language="JavaScript">
function help_bbc()
{
Help_BBC = window.open("<?php echo "$install_url";?>/help/help_bbcode.php", null, "scrollbars=yes,menu=no,resizable=yes,status=yes,height=500,top=70,width=500");
}
</script> 

<form name="recipes" method="POST" action="<?php echo $install_url;?>/admin/edit.php?mode=edit&rec_id=<?php echo $rec_id; ?>">
   <table cellpadding="0" cellspacing="0">
   <tr>
   <td width="25%">
   Recipe Name:
   </td>
   <td width="75%">
   <input type="text" name="rec_name" value="<?php echo $row[rec_name];?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Source / By:
   </td>
   <td width="75%">
   <input type="text" name="rec_source" value="<?php echo $row[rec_source];?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Catigory:
   </td>
   <td width="75%">
   <input type="text" name="rec_cat" value="<?php echo $row[rec_cat];?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Main Ingredient:
   </td>
   <td width="75%">
   <input type="text" name="rec_main" value="<?php echo $row[rec_main];?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Prep-Time:
   </td>
   <td width="75%">
   <input type="text" name="rec_prep" value="<?php echo $row[rec_prep];?>" size="35" maxlength="64">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Cook-Time:
   </td>
   <td width="75%">
   <input type="text" name="rec_cook" value="<?php echo $row[rec_cook];?>" size="35" maxlength="64">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Servings:
   </td>
   <td width="75%">
   <input type="text" name="rec_serves" value="<?php echo $row[rec_serves];?>" size="35" maxlength="64">
   </td>
   </tr>
   <td width="25%">
   Quick Meal:
   </td>
   <td width="75%">
   <input type="radio" name="rec_qm" <?php if($row[rec_qm]=="1")echo "checked ";?> value="1">Yes <input type="radio" name="rec_qm" <?php if($row[rec_qm]=="0")echo "checked ";?> value="0">No 
   </td>
   </tr>
   <tr>
   <td width="25%">
   Ingredients:
   </td>
   <td width="75%">
   <textarea name="rec_ingred" rows="5" cols="50"><?php echo $row[rec_ingred]; ?></textarea><br />
   <input type="button" value="Help: BBCode" onClick="help_bbc();"/>
   </td>
   </tr>
   <tr>
   <td width="25%">
   Directions:
   </td>
   <td width="75%">
   <textarea name="rec_direct" rows="5" cols="50"><?php echo $row[rec_direct]; ?></textarea><br />
   <input type="button" value="Help: BBCode" onClick="help_bbc();"/>
   </td>
   </tr>
   <tr>
   <td width="25%">
   Notes:
   </td>
   <td width="75%">
   <textarea name="rec_notes" rows="5" cols="50"><?php echo $row[rec_notes]; ?></textarea><br />
   <input type="button" value="Help: BBCode" onClick="help_bbc();"/>
   </td>
   </tr>
   <tr>
   <td>
   </td>
   <td>
   <input type="submit" value="Submit"><input type="reset" value="Reset"><input type="button" value="Check Spelling" onClick="openSpellChecker();"/>
   </td>
   </tr>
   </table>
</form>
<?php
	 }
}
elseif ($mode == "edit") {
	   	 //$rec_id = $_POST['rec_id'];
    	 $rec_name = $_POST['rec_name'];
		 $rec_cat = $_POST['rec_cat'];
		 $rec_main = $_POST['rec_main'];
		 $rec_prep = $_POST['rec_prep'];
		 $rec_cook = $_POST['rec_cook'];
		 $rec_serves = $_POST['rec_serves'];
		 $rec_source = $_POST['rec_source'];
		 $rec_qm = $_POST['rec_qm'];
		 $rec_ingred = $_POST['rec_ingred'];
		 $rec_direct = $_POST['rec_direct'];
		 $rec_notes = $_POST['rec_notes'];
		 
		 $query = "UPDATE `recipe` SET `rec_name`='$rec_name', `rec_cat`='$rec_cat', `rec_main`='$rec_main', `rec_prep`='$rec_prep', `rec_cook`='$rec_cook', `rec_serves`='$rec_serves', `rec_source`='$rec_source', `rec_qm`='$rec_qm', `rec_ingred`='$rec_ingred', `rec_direct`='$rec_direct', `rec_notes`='$rec_notes' WHERE `rec_id`='$rec_id' LIMIT 1";
		 mysql_query($query) or die(mysql_error());
		 
		 ?>
		 Edited recipe in dB, <a href="<?php echo $install_url;?>/admin/index.php">Back to Admin Home</a>
		 <script language="Javascript">
		 <!--
		 var URL = "./";
		 var speed = '0';
		 function reload() {
		 location = URL;
		 }
		 setTimeout("reload()", speed);
		 //-->
		 </script>
		 <?php
}
else {
	 echo "Error unknown mode!";
}
?>
</div>
</td>
</tr>
<tr>
<td colspan="2" class="foot">
<?php
//adds footer
include ("$install_dir/include/footer.php");

?>
</td>
</tr>
</table>
</center>
</body>
</html>
